package com.retail.product.service.impl;

import com.retail.common.core.exception.GlobalException;
import com.retail.common.core.utils.DateUtils;
import com.retail.common.core.utils.StringUtils;
import com.retail.product.common.domain.TBrand;
import com.retail.product.mapper.TBrandMapper;
import com.retail.product.service.ITBrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 品牌Service业务层处理
 *
 * @author retail
 * @date 2024-02-26
 */
@Service
public class TBrandServiceImpl implements ITBrandService {
    private static final Logger log = LoggerFactory.getLogger (TBrandServiceImpl.class);

    @Autowired
    private TBrandMapper tBrandMapper;

    /**
     * 查询品牌
     *
     * @param brandId 品牌主键
     * @return 品牌
     */
    @Override
    public TBrand selectTBrandByBrandId(Integer brandId) {
        return tBrandMapper.selectTBrandByBrandId (brandId);
    }

    /**
     * 查询品牌列表
     *
     * @param tBrand 品牌
     * @return 品牌
     */
    @Override
    public List<TBrand> selectTBrandList(TBrand tBrand) {
        return tBrandMapper.selectTBrandList (tBrand);
    }

    /**
     * 新增品牌
     *
     * @param tBrand 品牌
     * @return 结果
     */
    @Override
    public int insertTBrand(TBrand tBrand) {
        tBrand.setCreateTime (DateUtils.getNowDate ());
        return tBrandMapper.insertTBrand (tBrand);
    }

    /**
     * 修改品牌
     *
     * @param tBrand 品牌
     * @return 结果
     */
    @Override
    public int updateTBrand(TBrand tBrand) {
        tBrand.setUpdateTime (DateUtils.getNowDate ());
        return tBrandMapper.updateTBrand (tBrand);
    }

    /**
     * 批量删除品牌
     *
     * @param brandIds 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deleteTBrandByBrandIds(Integer[] brandIds) {
        return tBrandMapper.deleteTBrandByBrandIds (brandIds);
    }

    /**
     * 删除品牌信息
     *
     * @param brandId 品牌主键
     * @return 结果
     */
    @Override
    public int deleteTBrandByBrandId(Integer brandId) {
        return tBrandMapper.deleteTBrandByBrandId (brandId);
    }

    @Override
    public String importBrand(List<TBrand> brandList) {
        if (StringUtils.isNull (brandList) || brandList.size () == 0) {
            throw new GlobalException ("导入的表格内容不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder ();
        StringBuilder failMsg = new StringBuilder ();

        for (TBrand tBrand : brandList) {
            TBrand brand = tBrandMapper.selectBrandByBrandName (tBrand.getBrandName ());
            try {
                if (StringUtils.isNull (brand)) {
                    tBrandMapper.insertTBrand (tBrand);
                    successNum++;
                    successMsg.append ("<br/>" + successNum + "、品牌 " + tBrand.getBrandName () + "导入成功");
                } else {
                    failureNum++;
                    failMsg.append ("<br/>" + failureNum + "、品牌 " + tBrand + "已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、品牌 " + tBrand + "导入失败";
                failMsg.append (msg + e.getMessage ());
                log.error (msg, e);
            }
        }
        if (failureNum > 0) {
            failMsg.insert (0, "很抱歉，导入失败！共 " + failureNum + "条数据格式不合格");
            throw new GlobalException (failMsg.toString ());
        } else {
            successMsg.insert (0, "数据全部导入成功！共" + successNum + "条数据");
        }


        return successMsg.toString ();
    }
}
